package com.zhenghaijin.playbackservice.mapper;

import com.zhenghaijin.playbackservice.model.PlaybackRecord;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface PlaybackRecordMapper {
    
    @Select("SELECT * FROM playback_record WHERE user_id = #{userId}")
    @Results({
        @Result(property = "id", column = "id"),
        @Result(property = "userId", column = "user_id"),
        @Result(property = "animeId", column = "anime_id"),
        @Result(property = "episode", column = "episode"),
        @Result(property = "watchedAt", column = "watched_at")
    })
    List<PlaybackRecord> findByUserId(Long userId);

    @Select("SELECT * FROM playback_record WHERE user_id = #{userId} LIMIT #{pageSize} OFFSET #{offset}")
    List<PlaybackRecord> findByUserIdWithPagination(@Param("userId") Long userId, 
                                                  @Param("offset") int offset,
                                                  @Param("pageSize") int pageSize);

    @Insert("INSERT INTO playback_record(user_id, anime_id, episode, watched_at) " +
            "VALUES(#{userId}, #{animeId}, #{episode}, #{watchedAt})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(PlaybackRecord record);

    @Update("UPDATE playback_record SET user_id=#{userId}, anime_id=#{animeId}, episode=#{episode}, watched_at=#{watchedAt} WHERE id=#{id}")
    int update(PlaybackRecord record);

    @Delete("DELETE FROM playback_record WHERE id = #{id}")
    int deleteById(Long id);
}